window.onload = function () {
    var inputData = [];
    var allData = [];
    var titleData=[];
    init(inputData);
    function init(data){
        $.ajax({
            type: "POST",
            url: "/analyze/historyAverageUnitPriceData",
            data:data,
            success: function (msg){
                if(msg.status=="false"){
                    return
                }else{
                    allData=msg.resultList;
                    doCharts();
                }
            }
        });
    }
    function doCharts(){
        allData.forEach(function(data,index){
            titleData[index] = data.YEARQUARTER;
        });
        var option4 = {
            baseOption: {
                timeline: {
                    // y: 0,
                    axisType: 'category',
                    // realtime: false,
                    // loop: false,
                    autoPlay: false,
                    currentIndex: titleData.length-1,
                    playInterval: 1000,
                    // controlStyle: {
                    //     position: 'left'
                    // },
                    data: titleData,
                    label: {
                        normal: {
                            color: '#ADADAD',
                        },
                    },
                },
                // title: {
                //     subtext: '数据来自国家统计局'
                // },
                tooltip: {
                },
                calculable : true,
                grid: {
                    top: 80,
                    bottom: 100,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: '',
                            label: {
                                show: false,
                                formatter: function (params) {
                                    return params.value.replace('\n', '');
                                }
                            }
                        }
                    }
                },
                xAxis: [
                    {
                        'type':'category',
                        'axisLabel':{'interval':0},
                        'data':[],
                        splitLine: {show: false},
                        axisLine:{
                            lineStyle:{
                                color:'#ADADAD',
                            }
                        },
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '单位（元）',
                        axisLine:{
                            lineStyle:{
                                color:'#ADADAD',
                            }
                        },
                    }
                ],
                series: [
                    {name: '平均单价', type: 'bar'}
                ]
            },
            options: getOptions()
        };
        var myChart = echarts.init($('#myChart4').get(0));
        myChart.setOption(option4);
        window.onresize = function () {
            myChart.resize()
        };
    }
    function getOptions() {
        var option = [];
        allData.forEach(function(data,index){
            var series = [];
            var xAxisData =[];
            var seriesData=[];
            data.PRICELIST.forEach(function (data,index) {
                // xAxisData[index]=data.NAME;
                xAxisData[index]=data.name;
                var ojb={
                    // name: data.NAME,
                    name: data.name,
                    // value:data.PRICE.toFixed(0)
                    value:data.price.toFixed(0)
                };
                seriesData[index] = ojb;
            });
            var xAxis = {
                data:xAxisData
            };
            series[0] ={
                data:seriesData
            };
            var dataMap ={
                xAxis:xAxis,
                series:series
            };
            option[index] = dataMap;
        });
        return option;
    }
    //搜索
    $('#searchData').click(function(){
        var citys = $('#city').val(),
            districts = $('#district').val(),
            loansBalanceStarts = $('#loansBalanceStart').val(),
            loansBalanceEnds =  $('#loansBalanceEnd').val(),
            loansAmountStarts = $('#loansAmountStart').val(),
            loansAmountEnds = $('#loansAmountEnd').val(),
            startDateStarts = $('#startDateStart').val(),
            startDateEnds = $('#startDateEnd').val(),
            loansDurations = $('#loansDuration').val(),
            risk = $('#risk').val(),
            buildAreaEnds = $('#buildAreaEnd').val(),
            buildAreaStarts = $('#buildAreaStart').val(),
            endDateStarts = $('#endDateStart').val(),
            endDateEnds = $('#endDateEnd').val(),
            inputData = {
                city:citys,
                district:districts,
                loansBalanceStart:loansBalanceStarts,
                loansBalanceEnd:loansBalanceEnds,
                loansAmountStart:loansAmountStarts,
                loansAmountEnd:loansAmountEnds,
                startDateStart:startDateStarts,
                startDateEnd:startDateEnds,
                loansDuration:loansDurations,
                risk:risk,
                buildAreaEnd:buildAreaEnds,
                buildAreaStart:buildAreaStarts,
                endDateStart:endDateStarts,
                endDateEnd:endDateEnds
            };
        init(inputData)
    });
};